#include "changepassword.h"
#include "ui_changepassword.h"



#include<QFile>

#include<QTextStream>
#include<QMessageBox>
#include<QDir>
#include <QRegularExpression>


ChangePassword::ChangePassword(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ChangePassword)
{
    ui->setupUi(this);

    setWindowTitle("修改密码");

    ui->NewPassword->setPlaceholderText("请输入新密码");

    ui->EnsurePassword->setPlaceholderText("请确认新密码");
}

ChangePassword::~ChangePassword()
{
    delete ui;
}

void ChangePassword::setUsername(const QString &username)
{
    m_username=username;
}
void ChangePassword::on_Ensure_clicked()
{
    QString newPassword = ui->NewPassword->text().trimmed();

    QString confirmPassword = ui->EnsurePassword->text().trimmed();


    // 1. 输入检查

    if (newPassword.isEmpty())
    {
        QMessageBox::warning(this, "输入错误", "请输入新密码！");
        return;
    }

    if (confirmPassword.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请确认新密码！");
        return;
    }


    // 2. 密码一致性检查

    if (newPassword != confirmPassword)
    {
        QMessageBox::warning(this, "密码不一致", "两次输入的密码不一致，请重新输入！");
        ui->EnsurePassword->clear();
        ui->EnsurePassword->setFocus();
        return;

    }

    // 3. 密码强度检查（可选）
    QRegularExpression rx("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{6,}$");

    if (!rx.match(newPassword).hasMatch())
    {
        QMessageBox::warning(this, "密码强度不足", "密码必须包含字母和数字，且不少于6位！");
        return;
    }


    // 4. 修改 users.txt 文件中的密码

    QFile file("users.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QMessageBox::critical(this, "文件错误", "无法读取用户数据文件！");
        return;
    }


    QTextStream in(&file);

    QStringList lines;

    bool userFound = false;


    while (!in.atEnd())
    {

        QString line = in.readLine().trimmed();
        if (line.isEmpty()) continue;
        QStringList parts = line.split("||", Qt::SkipEmptyParts);
        if (parts.size() == 3)
        {
            QString username = parts[0].trimmed();
            if (username == m_username)
            {
                // 找到用户，替换密码
                parts[1] = newPassword; // 替换密码
                lines.append(parts.join("||"));
                userFound = true;

            }
            else
            {
                lines.append(line); // 保留原行
            }
        }

    }


    file.close();


    // 5. 写回文件

    if (userFound)
    {

        QFile outFile("users.txt");
        if (!outFile.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            QMessageBox::critical(this, "写入失败", "无法保存密码更改！");
            return;
        }

        QTextStream out(&outFile);
        for (const QString &line : lines)
        {
            out << line << "\n";
        }

        outFile.close();
        QMessageBox::information(this, "修改成功", "密码已成功修改！");

        accept(); // 关闭对话框

    }
    else
    {
        QMessageBox::warning(this, "错误", "未找到该用户！");
    }
}


void ChangePassword::on_Before_clicked()
{
    reject();
}

